package cn.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.beans.Message;
import cn.utils.PageModel;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("unchecked")
public class MessageAction extends BaseAction {

	private Message message;
	private String messageId;
	private int currentPage;
	private int pageSize;
	private PageModel pm = new PageModel();
	private String keywords;
	private String errorinfo;
	private String code;
	
	/**
	 * 留言
	 * @throws Exception
	 */
	public void leaveMessage() throws Exception{
		try{
			if(null != code && !"".equals(code)){
				
				String sCode = (String)ActionContext.getContext().getSession().get("code");
				if(code.equals(sCode)){
					if(null != message){
						String name = message.getName();
						String content = message.getContent();
						
						if(null == name || "".equals(name.trim())){
							this.sendMsgAjax("name", null);
						}else if(null == content || "".equals(content.trim())){
							this.sendMsgAjax("content", null);
						}else{
							message.setCreDate(new Date());
							commonservice.saveDemo(message);
							
							this.sendMsgAjax("success", null);
						}
						
					}
				}else{
					this.sendMsgAjax("scode", null);
				}
			}else{
				this.sendMsgAjax("code", null);
			} 
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}
	
	/**
	 * 留言列表
	 * @return
	 */
	public String listMessage(){
		String sadminId = (String)ActionContext.getContext().getSession().get("adminId");
		if(null == sadminId){
			errorinfo = "Time Over";
			return LOGIN;
		}else{
			pm.setCurrentPage(currentPage);
			pm.setPageSize(pageSize);

			List listOne = new ArrayList();
			String[] propertyNameQuery = { "name","telephone","mobile","email","qq","msn","content" };
			String[] propertyConQuery = { "like","like","like","like","like","like","like" };
			Object[] propertyValueQuery = { keywords,keywords,keywords,keywords,keywords,keywords,keywords };
			String[] conditionQuery = { "OR","OR","OR","OR","OR","OR" };
			listOne.add(propertyNameQuery);
			listOne.add(propertyConQuery);
			listOne.add(propertyValueQuery);
			listOne.add(conditionQuery);

			String[] orderPropertyName = { "creDate" };
			String[] orderValue = { "DESC" };

			pm = commonservice.findDataByPage(pm, "Message", listOne, null, null, null, null, null, null, orderPropertyName, orderValue);

		}
		return SUCCESS;
	}
	
	/**
	 * 删除留言(含批量删除)
	 */
	public void deleteMessages() throws Exception{
		try{
			String adminId = (String)ActionContext.getContext().getSession().get("adminId");
			if(null == adminId){
				this.sendMsgAjax("login", null);
			}else{
				String[] ids = messageId.split(",");
				List list = new ArrayList();
				int length = ids.length;
				for(int i=0;i<length;i++){
					Message m = commonservice.findById(ids[i], new Message());
					if(null != m){
						list.add(m);
					}
				}
				commonservice.deleteAll(list);

				this.sendMsgAjax("success", null);
			}
		}catch (Exception e) {
			e.printStackTrace();
			this.sendMsgAjax("error", null);
		}
	}

	public Message getMessage() {
		return message;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getErrorinfo() {
		return errorinfo;
	}

	public void setErrorinfo(String errorinfo) {
		this.errorinfo = errorinfo;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
	
}
